package com.vf.admin.utils;

import java.util.Calendar;
import java.util.Date;

public class DateJisuan {

    private static final long ONE_MILLISECOND = 1000L;
    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;

    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "1个月前";
    private static final String TWO_MONTH_AGO = "2个月前";
    private static final String THREE_MONTH_AGO = "3个月前";
    private static final String FOUR_MONTH_AGO = "4个月前";
    private static final String FIVE_MONTH_AGO = "5个月前";
    private static final String SIX_MONTH_AGO = "6个月前";
    private static final String SEVEN_MONTH_AGO = "7个月前";
    private static final String EIGHT_MONTH_AGO = "8个月前";
    private static final String NINE_MONTH_AGO = "9个月前";
    private static final String TEN_MONTH_AGO = "10个月前";
    private static final String ELEVEN_MONTH_AGO = "11个月前";
    private static final String ONE_YEAR_AGO = "超1年未登录";

    /**
     * 默认增加5分钟
     * @param date
     * @return
     */
    public static String format(Date date) {

        Calendar calendar = Calendar.getInstance();
        long now = calendar.getTimeInMillis();
        long delta = now - date.getTime();

        // 逻辑判断
        if (delta < ONE_MINUTE) {
            return delta/ONE_MILLISECOND + ONE_SECOND_AGO;
        }else  if (delta < ONE_HOUR) {
            return delta/ONE_MINUTE + ONE_MINUTE_AGO;
        }else if (delta < ONE_DAY) {
            return delta/ONE_HOUR + ONE_HOUR_AGO;
        }else if (delta < getTime(calendar, now)) {
            return delta/ONE_DAY + ONE_DAY_AGO;
        }else if (delta < getTime(calendar, now)) {
            return ONE_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return TWO_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return THREE_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return FOUR_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return FIVE_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return SIX_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return SEVEN_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return EIGHT_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return NINE_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return TEN_MONTH_AGO;
        }else if (delta < getTime(calendar, now)) {
            return ELEVEN_MONTH_AGO;
        }
        return ONE_YEAR_AGO;
    }

    private static long getTime(Calendar calendar, long now) {
        calendar.add(Calendar.MONTH, -1);
        return now - calendar.getTimeInMillis();
    }
}
